package cn.csg.iotplatform.module.system.dal.mysql.dept;

import cn.csg.iotplatform.framework.mybatis.core.mapper.BaseMapperX;
import cn.csg.iotplatform.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csg.iotplatform.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.csg.iotplatform.module.system.controller.admin.dept.vo.dept.DeptTreeSearchVO;
import cn.csg.iotplatform.module.system.controller.admin.dept.vo.dept.DeptVO;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.*;
import cn.csg.iotplatform.module.system.controller.admin.user.vo.user.UserDeptRelationVo;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.DeptDO;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.DeptWithParentDO;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.RemoteDeptDO;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.UserDeptRelationDto;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.vo.DeptSearchVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

@Mapper
public interface DeptMapper extends BaseMapperX<DeptDO> {

    DeptDO selectDeptById(@Param("deptId") Long deptId);

    List<DeptDO> selectDeptList();

    default List<DeptDO> selectList(DeptListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DeptDO>()
                .likeIfPresent(DeptDO::getName, reqVO.getName())
                .eqIfPresent(DeptDO::getStatus, reqVO.getStatus()));
    }

    default DeptDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(DeptDO::getParentId, parentId);
    }

    default List<DeptDO> selectListByParentId(Collection<Long> parentIds) {
        return selectList(DeptDO::getParentId, parentIds);
    }

    IPage<DeptWithParentDO> selectDeptListWithParent(IPage<DeptWithParentDO> page, @Param("reqVO") DeptListReqVO reqVO);

    void batchUpdateUseFlag(@Param("ids") List<Long> ids, @Param("useFlag") Integer useFlag);

    List<RemoteDeptDO> selectRemoteDeptDataList();

    List<AdminAreaDO> selectAdminAreaSetting(@Param("deptId")Long deptId);

    List<AdminAreaDO> selectAdminAreaList(@Param("type")Integer type,@Param("provinceCode")String provinceCode);

    Integer insertDeptAreaRel(DeptAreaRelDO deptAreaRel);

    Integer deleteDeptAreaRel(@Param("deptId")Long deptId);

    void insertBatchDeptAreaRel(List<DeptAreaRelDO> list);

    List<UserDeptRelationVo> queryUserDeptRelation(@Param("userId") Long id);

    void insertUserDeptRelationBatch(@Param("list") List<UserDeptRelationDto> userDeptRelationDtoList);

    void deleteByUserIdAndDeptIds(@Param("userId") Long userId, @Param("deptIds") Collection<Long> deptIds);

    List<Long> selectPurviewDeptIds(@Param("userId") Long userId,@Param("targetUserId") Long targetUserId,@Param("useFlag") Integer useFlag,@Param("projectAttr") Integer projectAttr,@Param("keyword") String keyword);

    List<Long> selectSuperDeptIds(@Param("deptId")Long deptId);

    List<Long> selectChildDeptIds(@Param("searchVO")DeptSearchVO searchVO);

    List<DeptDO> selectChildDeptList(@Param("searchVO")DeptSearchVO searchVO);

    List<Long> selectChildDeptIdsByUserFlag(@Param("deptId")Long deptId, @Param("useFlag")Integer useFlag);

    List<Long> selectBelongDeptIdsByUserId(@Param("userId")Long userId);

    List<Long> selectSuperDeptIdsByUserId(@Param("userId")Long userId);

    List<Long> selectChildDeptIdsByUserId(@Param("userId")Long userId);

    List<DeptDO> selectSuperDeptTreeByKeyword(@Param("keyword")String keyword,@Param("useFlag") Integer useFlag);

    List<DeptDO> selectDeptTreeList(@Param("searchVO")DeptTreeSearchVO searchVO);

    List<Long> selectPurviewSuperDeptIds(@Param("userId")Long userId,@Param("list")List<Long> deptIds);

    List<Long> selectPurviewChildDeptIds(@Param("userId")Long userId,@Param("list")List<Long> deptIds);

    List<DeptDO> selectPurviewDeptTreeByDeptIds(@Param("list") List<Long> purviewDeptIds);

    List<Long> getChildDeptTreeWithUserAttr(@Param("deptId") Long deptId);

    UserDeptRelationDto selectUserDeptRelation(@Param("userId")Long userId, @Param("deptId")Long deptId);

    long selectMaxId();

    List<DeptDO> getDeptListWithParent();

    /**
     * 物理删除
     */
    void deleteAll();

    List<Long> getUserDeptRelationIds(@Param("userId") Long userId);

    List<Long> selectLeafProp(@Param("ids")List<Long> ids, @Param("propType")Integer value);

    List<DeptVO> selectUserDeptList(@Param("userId")Long userId);

    List<DeptDO> selectAllDeptTreeList(@Param("searchVO") DeptTreeSearchVO searchVO);
}
